package com.dxl.system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxl.base.Result;
import com.dxl.base.Results;
import com.dxl.system.mapper.GroupsMapper;
import com.dxl.system.mapper.LabelMapper;
import com.dxl.system.mapper.LabelNoteMapper;
import com.dxl.system.mapper.NoteMapper;
import com.dxl.system.model.Label;
import com.dxl.system.model.LabelNote;
import com.dxl.system.model.Note;
import com.dxl.system.service.LabelService;
import com.dxl.system.service.NoteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <b> 标签服务接口实现类
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-09-25    <br>Author : dxl
 */
@Service
public class LabelServiceImpl extends ServiceImpl<LabelMapper,Label> implements LabelService {

    @Autowired(required = false)
    private LabelMapper labelMapper;
    @Autowired(required = false)
    private LabelNoteMapper labelNoteMapper;
    @Autowired(required = false)
    private NoteService noteService;
    @Autowired(required = false)
    private GroupsMapper groupsMapper;
    @Autowired(required = false)
    private NoteMapper noteMapper;

    /**
     * <b> 插入标签
     * </b><br><br><i>Description</i> : 不能为空：标签名称-label_name ；不能重复：标签名称-label_name  
     * @param label Label
     * @return com.dxl.base.Result Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result addOne(Label label) {
        Result result = isLabelAddable(label);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        labelMapper.insert(label);
        return Results.successWithData(label);
    }

    /**
     * <b> 分配文件给标签
     * </b><br><br><i>Description</i> :
     * @param labelId Integer，noteIds -List: Integer
     * @return com.dxl.base.Result Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    @Transactional
    public Result addNoteToLabel(Integer labelId, List<Integer> noteIds) {
        for(Integer noteId : noteIds){
            LabelNote labelNote = new LabelNote();
            labelNote.setLabelId(labelId);
            labelNote.setNoteId(noteId);
            QueryWrapper<LabelNote> labelNoteWrapper = new QueryWrapper<LabelNote>(labelNote);
            if(labelNoteMapper.selectList(labelNoteWrapper).size() == 0){
                labelNoteMapper.insert(labelNote);
            }
        }
        return Results.successWithData(true);
    }

    /**
     * <b> 删除标签
     * </b><br><br><i>Description</i> : 
     * @param labelIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result delByIds(List<Integer> labelIds) {
        int num = labelMapper.deleteBatchIds(labelIds);
        for(Integer id : labelIds){
            delAllNoteFromLabel(id);
        }
        return Results.successWithData(num);
    }


    /**
     * <b> 移除标签中文件
     * </b><br><br><i>Description</i> :
     * @param labelId Integer，noteIds -List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    @Transactional
    public Result delNoteFromLabel(Integer labelId, List<Integer> noteIds) {
        int num = 0;
        for(Integer noteId : noteIds){
            LabelNote labelNote = new LabelNote();
            labelNote.setLabelId(labelId);
            labelNote.setNoteId(noteId);
            QueryWrapper<LabelNote> labelNoteWrapper = new QueryWrapper<LabelNote>(labelNote);
            labelNoteMapper.delete(labelNoteWrapper);
            num++;
        }
        return Results.successWithData(num);
    }

    /**
     * <b> 移除标签中全部文件
     * </b><br><br><i>Description</i> :
     * @param labelId Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    @Transactional
    public Result delAllNoteFromLabel(Integer labelId) {
        LabelNote labelNote = new LabelNote();
        labelNote.setLabelId(labelId);
        QueryWrapper<LabelNote> labelNoteWrapper = new QueryWrapper<LabelNote>(labelNote);
        int num = labelNoteMapper.delete(labelNoteWrapper);
        return Results.successWithData(num);
    }

    /**
     * <b> 编辑标签
     * </b><br><br><i>Description</i> : 不能重复：标签名称-label_name 
     * @param label Label
     * @return com.dxl.base.Result Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result editOne(Label label) {
        Result result = isLabelEditable(label);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        labelMapper.updateById(label);
        return Results.successWithData(label);
    }

    /**
     * <b> ID查询标签
     * </b><br><br><i>Description</i> : 
     * @param labelId Integer
     * @return com.dxl.base.Result: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getById(Integer labelId) {
        Label label = labelMapper.selectById(labelId);
        return Results.successWithData(label);
    }

    /**
     * <b> 条件查询标签
     * </b><br><br><i>Description</i> : 
     * @param label Label
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getList(Label label) {
        QueryWrapper<Label> labelWrapper = new QueryWrapper<Label>(label);
        labelWrapper.orderByDesc("id");
        List<Label> labelList = labelMapper.selectList(labelWrapper);
        colorLabel(labelList);
        return Results.successWithData(labelList);
    }

    /**
     * <b> 分页条件查询标签
     * </b><br><br><i>Description</i> : 
     * @param label Label, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result getListByPage(Label label, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Label> labelList = (List<Label>) getList(label).getData();
        PageInfo<Label> pageInfo = new PageInfo<Label>(labelList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 查询标签中全部文件
     * </b><br><br><i>Description</i> : 联表
     * @param labelId Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getAllNoteInLabel(Integer labelId) {
        Label label = labelMapper.selectById(labelId);
        List<Note> noteList = labelMapper.getAllNoteInLabel(labelId);
        List<Note> noteList2 = new ArrayList<>();
        Note note1 = new Note();
        for(Note note : noteList){
            note1.setId(note.getId());
            QueryWrapper<Note> noteQueryWrapper = new QueryWrapper<>();
            noteQueryWrapper.eq("note.id",note.getId());
            noteList2.addAll(noteMapper.getJoinLabelSeries(noteQueryWrapper));
        }
        noteList2 = noteService.handleNote(noteList2);
        return Results.successWithData(noteList2);
    }

    /**
     * <b> 分页查询标签中全部文件
     * </b><br><br><i>Description</i> : 联表
     * @param labelId Integer, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Note
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getAllNoteInLabelByPage(Integer labelId, Integer currentPage, Integer pageSize) {
        Label label = labelMapper.selectById(labelId);
        PageHelper.startPage(currentPage, pageSize);
        List<Note> noteList = labelMapper.getAllNoteInLabel(labelId);

        List<Note> noteList2 = new ArrayList<>();
        for(Note note : noteList){
            QueryWrapper<Note> noteQueryWrapper = new QueryWrapper<>();
            noteQueryWrapper.eq("note.id",note.getId());
            noteList2.addAll(noteMapper.getJoinLabelSeries(noteQueryWrapper));
        }
        noteList2 = noteService.handleNote(noteList2);
        PageInfo<Note> pageInfo = new PageInfo<Note>(noteList2);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 模糊查询标签
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：label_name-标签名称 
     * @param label Label
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25    <br>Author : dxl
     */
    @Override
    public Result getLikeLabel(Label label) {
        QueryWrapper<Label> labelWrapper = new QueryWrapper<>();
        if(label.getLabelName() != null){
            labelWrapper.like("label_name",label.getLabelName());
        }else{
            return Results.failure("请正确填写属性:(");
        }
        List<Label> labelList = labelMapper.selectList(labelWrapper);
        labelList = colorLabel(labelList);
        return Results.successWithData(labelList);
    }
    /**
     * <b> 分页模糊查询标签
     * </b><br><br><i>Description</i> : 模糊查询；模糊字段(模式：单字段)：全匹配：label_name-标签名称 
     * @param label Label, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Label
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result getLikeLabelByPage(Label label, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Label> labelList = (List<Label>) getLikeLabel(label).getData();
        PageInfo<Label> pageInfo = new PageInfo<Label>(labelList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 标签能否新增
     * </b><br><br><i>Description</i> : 不能为空：标签名称-label_name ；不能重复：标签名称-label_name  
     * @param label Label
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result isLabelAddable(Label label) {

        if(label.getLabelName() == null){
            return Results.successWithData(false,"标签名称 不能为空:(");
        }

        if(label.getLabelName() != null){
        QueryWrapper<Label> labelWrapper = new QueryWrapper<>();
        labelWrapper.eq("label_name",label.getLabelName());
        List<Label> labelList = labelMapper.selectList(labelWrapper);
            if(labelList.size() > 0){
                return Results.successWithData(false,"标签名称 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }

    /**
     * <b> 标签能否编辑
     * </b><br><br><i>Description</i> : 不能重复：标签名称-label_name 
     * @param label Label
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-09-25     <br>Author : dxl
     */
    @Override
    public Result isLabelEditable(Label label) {

        if(label.getLabelName() != null){
            QueryWrapper<Label> labelWrapper1 = new QueryWrapper<>();
            labelWrapper1.eq("id",label.getId()).eq("label_name",label.getLabelName());
            List<Label> labelList1 = labelMapper.selectList(labelWrapper1);
            QueryWrapper<Label> labelWrapper2 = new QueryWrapper<>();
            labelWrapper2.eq("label_name",label.getLabelName());
            List<Label> labelList2 = labelMapper.selectList(labelWrapper2);
            if(!((labelList1.size() == 1 && labelList2.size() == 1) ||(labelList1.size() == 0 && labelList2.size() == 0))){
                return Results.successWithData(false,"标签名称 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }

    /**
    * <b> 给标签字体、边框随机颜色
    * </b><br><br><i>Description</i> : 
    * @param labelList
    * @return java.util.List<com.dxl.system.model.Label>
    * <br><br>Date: 2019/9/29 11:00     <br>Author : dxl 
    */
    @Override
    public List<Label> colorLabel(List<Label> labelList){
        List<String> fontColorList = new ArrayList<>();
        fontColorList.add("#6f42c1");
        fontColorList.add("#0f0f0f");
        fontColorList.add("#1b6d85");
        fontColorList.add("#002b36");
        fontColorList.add("#00B83F");
        fontColorList.add("#2b542c");
        fontColorList.add("#122b40");
        fontColorList.add("#761c19");
        fontColorList.add("#8a6d3b");
        fontColorList.add("#A705ED");
        fontColorList.add("#cb4b16");
        fontColorList.add("blue");
        fontColorList.add("maroon");
        fontColorList.add("dodgerblue");
        String[] char16 = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
        for(Label label : labelList){
            label.setLabelFontColor(fontColorList.get((int)(1+Math.random()*13)));
            StringBuffer sb = new StringBuffer();
            sb.append("#");
            for(int i = 0; i < 6; i++){
                sb.append(char16[(int)(1+Math.random()*15)]);
            }
            label.setLabelSolidColor(sb.toString());
        }

        return labelList;
    }
}
